﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class InvMoveList : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
            BuildGridPanel(this.GridPanel4, mid, Session["UserID"].ToString());
            //this.comShop.GetStore().DataSource = QueryShop("");
            this.txtbdate.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
            this.txtedate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.DateField2.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
            this.DateField3.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.StoreColumns(this.Store1, mid, "GridView1");
            this.shoplist.GetStore().DataSource = mydb.DM_GetDateSet("select shopname,shopid,address,AreaName from V_ShopList");
            this.vendorlist.GetStore().DataSource = mydb.DM_GetDateSet("select vendor from bas_vendor");
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string str = " and outdate between '" + txtbdate.Text + "' and '" + txtedate.Text + "'";
        if (this.txtstate.Text != "0")
        {
            str +=" and MoveSate = '" + this.txtstate.SelectedItem.Text + "'";
        }
        if (this.instockname.Text != "")
        {
            str += " and instockname like '%" + instockname.Text + "%'";
        }
        if (this.outstockname.Text != "")
        {
            str += " and outstockname like '%" + outstockname.Text + "%'";
        }
        if (this.txttype.SelectedItem.Text == "物料")
        {
            str += " and kindname ='物料'";
        }
        if (this.txttype.SelectedItem.Text == "货品")
        {
            str += " and kindname <>'物料'";
        }
        string sql = @"select *from KM_V_Inv_MoveList where 1=1 "+str;
        this.StoreColumns(this.Store1, mid, "GridView1");
        this.Store1.DataSource = db.DM_GetDateSet(sql);
        this.Store1.DataBind();
    }
    protected void BtnQureyUser2(object sender, DirectEventArgs e)
    {
        string str = " and recdate between '" + DateField2.Text + "' and '" + DateField3.Text + "' ";

        string sql = @"select *from Bas_StockExpen where 1=1 " + str;
        this.Store3.DataSource = mydb.DM_GetDateSet(sql);
        this.Store3.DataBind();
    }
    protected void BtnGetUser(object sender, DirectEventArgs e)
    {
        string sql = @"insert into Bas_ShopBegin
                        select a.ShopCode,a.ShopName,'','','',0,0.4,0.4,0.35,0.35,0.12,getdate(),'"+Session["UserCode"]+@"'  from V_Shop a 
                        left join Bas_ShopBegin b on b.ShopCode = a.ShopCode
                        where isnull(b.ShopCode,'') = ''";
        if(mydb.DM_ExecuteNonQuery(sql)!=-1){
            ShowBox("加载成功！");
        }else{
            ShowBox("加载失败！");
        }
        BtnQureyUser(null, null);
    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void Edit2(string SeqCode, string field, string oldValue, string newValue, object customer)
    {
        newValue = newValue.Replace("\"", "");
        if (mydb.DM_ExecuteNonQuery("update Bas_StockExpen set " + field + "='" + newValue + "' where SeqCode='" + SeqCode + "'") != -1)
        {
            this.GridPanel2.GetStore().CommitChanges();
            if (field == "ShipName")
            {
                DataSet ds = mydb.DM_GetDateSet("select * from V_ShopList where shopname = '" + newValue + "'");
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    mydb.DM_ExecuteNonQuery("update Bas_StockExpen set shopid='" + ds.Tables[0].Rows[0]["shopid"] 
                        + "',cityname = '" + ds.Tables[0].Rows[0]["AreaName"] 
                        + "',shopaddrr = '" + ds.Tables[0].Rows[0]["address"] + "' where SeqCode='" + SeqCode + "'");
                    this.Store2.DataSource = mydb.DM_GetDateSet("select *from Bas_StockExpen where SeqCode ='" + SeqCode + "'");
                    this.Store2.DataBind();
                }
            }
            if (field == "Vendor")
            {
                DataSet ds = mydb.DM_GetDateSet("select * from bas_vendor where vendor = '" + newValue + "'");
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    mydb.DM_ExecuteNonQuery("update Bas_StockExpen set priced='" + ds.Tables[0].Rows[0]["priced"]
                        + "',pricex = '" + ds.Tables[0].Rows[0]["pricex"]
                        + "' where SeqCode='" + SeqCode + "'");
                    this.Store2.DataSource = mydb.DM_GetDateSet("select *from Bas_StockExpen where SeqCode ='" + SeqCode + "'");
                    this.Store2.DataBind();
                }
            }
            if (field == "QtyD" || field == "QtyX")
            {
                mydb.DM_ExecuteNonQuery("update Bas_StockExpen set moneyd= isnull(qtyd,0)*isnull(priced,0)"
                        + ",moneyx = isnull(qtyx,0)*isnull(pricex,0)"
                        + ",totalmoney = isnull(qtyd,0)*isnull(priced,0) + isnull(qtyx,0)*isnull(pricex,0) where SeqCode ='" + SeqCode + "'");
                this.Store2.DataSource = mydb.DM_GetDateSet("select *from Bas_StockExpen where SeqCode ='" + SeqCode + "'");
                this.Store2.DataBind();
            }
        }
        else
        {
            X.Msg.Notify(new NotificationConfig()
            {
                Title = "消息提示",
                Html = "修改失败！",
                Width = 250
            }).Show();
        }
    }
    protected void BtnShowExpen(object sender, DirectEventArgs e)
    {
        string str ="";
        string code = GetSysCode("WF", DateTime.Now.ToString("yyyyMMdd"), "Bas_StockExpen", "SeqCode");
        RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        foreach(SelectedRow row in sm.SelectedRows){
            str += " insert into Bas_StockExpenDtl values('" + code + "','" + row.RecordID + "')";
        }
        //ShowBox(code);
        string sql = " insert into Bas_StockExpen(SeqCode,Creater,CreateDate)select '" + code + "','"+Session["UserCode"]+"',getdate()";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            mydb.DM_ExecuteNonQuery(str);
            this.Store2.DataSource = mydb.DM_GetDateSet("select *from Bas_StockExpen where SeqCode ='" + code + "'");
            this.Store2.DataBind();
            OpenExpenDtl(code);
            this.Window1.Maximized = true;
            this.Window1.Show();
        }
        else
        {
            ShowBox("出现错误！");
        }
    }
    protected void BtnSave(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        if (sm.SelectedRow == null)
        {
            ShowBox("信息录入不完整，无法保存！");
            return;
        }
        string code = sm.SelectedRow.RecordID;
        DataSet ds = mydb.DM_GetDateSet("select *from Bas_StockExpen where SeqCode = '"+code+"'");
        if (ds.Tables[0].Rows[0]["recdate"].ToString() == "")
        {
            ShowBox("费用日期不能为空！");
            return;
        }
        if(ds.Tables[0].Rows[0]["Vendor"].ToString() ==""){
            ShowBox("物流供应商不能为空！");
            return;
        }if(ds.Tables[0].Rows[0]["SendCode"].ToString() ==""){
            ShowBox("运单号不能为空！");
            return;
        }
        if(ds.Tables[0].Rows[0]["ShipName"].ToString() ==""){
            ShowBox("收货人不能为空！");
            return;
        }
        if (ds.Tables[0].Rows[0]["KindName"].ToString() == "")
        {
            ShowBox("货品类别不能为空！");
            return;
        } if (ds.Tables[0].Rows[0]["QtyD"].ToString() == "" && ds.Tables[0].Rows[0]["QtyX"].ToString() == "")
        {
            ShowBox("箱数不能为空！");
            return;
        }
        this.Window1.Hide();
        BtnQureyUser(null, null);
    }

    protected string GetSysCode(string mm, string dastr,string tabname,string fieldname)
    {
        string sql = @"select  '" + mm + "'+'" + dastr + "'+isnull(right(cast(cast(right(max(" + fieldname + @"),3) as int)+10001 
                        as varchar(50)),4),'0001') from " + tabname+" where "+fieldname+" like '"+mm+dastr+"%'";
        return mydb.DM_GetExecuteScalarObj(sql).ToString();
    }
    protected void Cell_Click(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        //ShowBox(sm.SelectedRow.RecordID);
        OpenExpenDtl(sm.SelectedRow.RecordID);
        
    }
    protected void OpenExpenDtl(string code)
    {
        string sql = @"select a.*from KM_V_Inv_MoveList a inner join [192.168.10.90].km_rpt.dbo.Bas_StockExpenDtl b on b.item_id = a.item_id where seqcode= '" + code + "'";
        this.StoreColumns(this.Store5, mid, "GridView1");
        this.Store5.DataSource = db.DM_GetDateSet(sql);
        this.Store5.DataBind();
    }
    protected void Colose_Click(object sender, DirectEventArgs e)
    {
        X.Msg.Confirm("温馨提示", "费用还没有录入完，是否要退出?", new JFunction { Fn = "showResult" }).Show();
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void DeleteExpan()
    {
        string sql = "delete from Bas_StockExpen where isnull(SendCode,'') =''";
        mydb.DM_ExecuteNonQuery(sql);
        this.Window1.Hide();
    }
}